/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jgaptest;

import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
import org.jgap.*;
import org.jgap.impl.IntegerGene;

/**
 *
 * @author jkanazawa
 */
public class JGapTest {

    private static BufferedImage img0;
    private static BufferedImage imgX;
    private static int cant_genes = 12;
    private static int nroOperadorIni = 0;
    private static int nroOperadorFin = 20;
    private static int tam_poblacion = 50;
    private static int cant_max_generaciones = 50;

    public static void main(String[] args) throws Exception {

        BufferedImage imgT0 = ImageIO.read(new File("C:\\Users\\Claudio\\Desktop\\Tesis\\Apps\\imgs\\pruebas\\cman.jpg")); // original
        BufferedImage imgTX = ImageIO.read(new File("C:\\Users\\Claudio\\Desktop\\Tesis\\Apps\\imgs\\pruebas\\cman2.jpg")); // final

        
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorConvertOp opera = new ColorConvertOp(cs, null);
        BufferedImage img0 = opera.filter(imgT0, null);
        BufferedImage imgX = opera.filter(imgTX, null);
        
        Configuration confDefault = new ConfiguracionAG();

        FitnessFunction miFF = new MaeFitnessFunction(img0, imgX);

        confDefault.setFitnessFunction(miFF);

        Gene[] genes = new Gene[cant_genes];

        for (int i = 0; i < cant_genes; i++) {
            genes[i] = new IntegerGene(confDefault, nroOperadorIni, nroOperadorFin);
        }

        Chromosome cromosoma = new Chromosome(confDefault, genes);
        confDefault.setSampleChromosome(cromosoma);

        confDefault.setPopulationSize(tam_poblacion);

        Genotype poblacion = Genotype.randomInitialGenotype(confDefault);

        for (int i = 0; i < cant_max_generaciones; i++) {
            poblacion.evolve();
        }

        IChromosome mejorIndividuo = poblacion.getFittestChromosome();

        Operador op = new Operador(img0);
        int nop;
        System.out.println("Mejor Secuencia de Operadores: ");
        int[][] dataTest = Operations.loadData(img0);
        for (int i = 0; i < cant_genes; i++) {
            nop = ((Integer) mejorIndividuo.getGene(i).getAllele()).intValue();
            System.out.println("Op: " + nop);
            dataTest = op.aplicarOperacion(nop, dataTest);
        }
        BufferedImage imgTestD = new BufferedImage(img0.getWidth(), img0.getHeight(), img0.getType());
        WritableRaster wr = imgTestD.getRaster();
        for (int i = 0; i < img0.getWidth(); i++) {
            for (int j = 0; j < img0.getHeight(); j++) {
                wr.setSample(i, j, 0, dataTest[i][j]);
            }
        }
        System.out.println("Fitness: " + mejorIndividuo.getFitnessValue());
        ImageIO.write(imgTestD, "JPG", new File("C:\\Users\\Claudio\\Desktop\\Tesis\\Apps\\imgs\\pruebas\\OUT.jpg"));

    }

    public static void main2(String[] args) throws Exception {
        BufferedImage imgTest = ImageIO.read(new File("C:\\Users\\Claudio\\Desktop\\Tesis\\Apps\\imgs\\pruebas\\man.jpg")); // original


        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorConvertOp opera = new ColorConvertOp(cs, null);
        BufferedImage image1 = opera.filter(imgTest, null);
        //BufferedImage image2 = oper.filter(pic2, null);
        BufferedImage imgTestD = new BufferedImage(imgTest.getWidth(), imgTest.getHeight(), image1.getType());
        //BufferedImage imgDest = new BufferedImage(imgTest.getWidth(), imgTest.getHeight(), image1.getType());

        WritableRaster wr = imgTestD.getRaster();
        int[][] dataTest = Operations.loadData(image1);
        int[] oper = {2, 2, 2};

        Operador op = new Operador(image1);
        for (int i = 0; i < oper.length; i++) {
            //nop = ((Integer) mejorIndividuo.getGene(i).getAllele()).intValue();
            //System.out.println("Op: " + nop);

            dataTest = op.aplicarOperacion(oper[i], dataTest);

        }

        for (int i = 0; i < image1.getWidth(); i++) {
            for (int j = 0; j < image1.getHeight(); j++) {
                wr.setSample(i, j, 0, dataTest[i][j]);
            }
        }
//dataTest1 = new int[imgTest.getWidth()][imgTest.getHeight()];
        // System.out.println("Fitness: " + Operations.distancia(dataTest1, dataTest, imgTest.getWidth(), imgTest.getHeight()));
        ImageIO.write(imgTestD, "JPG", new File("C:\\Users\\Claudio\\Desktop\\Tesis\\Apps\\imgs\\pruebas\\OUT.jpg"));

    }
}
